<template>
  <div id="resourceManagement">
    <div id="tabelTitle">
      资源管理
    </div>
    <div id="table">
      <avue-crud
        ref="crud"
        v-model="form"
        :search.sync="searchForm"
        :page.sync="page"
        :data="tableData"
        :table-loading="tableLoading"
        :option="tableOption"
        @selection-change="selectionChange"
        @on-load="getList"
        @search-change="searchChange"
        @refresh-change="refreshChange"
        @size-change="sizeChange"
        @current-change="currentChange"
        @row-update="handleUpdate"
        @row-save="handleSave"
        @row-del="rowDel">
        <template slot-scope="{disabled,size}" slot="stuNameSearch">
          <div class="inputStyle">
            <el-input placeholder="请输入姓名或手机号" :disabled="disabled" :size="size" style="width:50%; "
                      v-model="searchForm.keyWord"></el-input>
          </div>
        </template>
        <template slot-scope="{disabled,size}" slot="adminIdSearch">
          <div>
            <el-select v-model="searchForm.adminId" placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.adminId"
                :label="item.adminRealName"
                :value="item.adminId">
              </el-option>
            </el-select>
          </div>
        </template>
        <template slot="menuLeft" slot-scope="{size}">
          <el-button type="primary" :size="size" @click="rowDel()">删除</el-button>
          <el-button type="primary" :size="size" @click="uploadExcel">导入</el-button>
          <el-button type="primary" :size="size" @click="grab">抓取</el-button>
        </template>
        <template slot="menu" slot-scope="{row,index}">
          <el-button type="primary" v-if="row.status !==110" size="medium"  @click="changeConsultant(row)">变更咨询师</el-button>
          <el-button type="primary"  v-if="row.status !==110" size="medium" @click.stop="$refs.crud.rowAdd()">修改</el-button>
          <el-button type="primary"  size="medium" @click.stop="$refs.crud.rowView(row,index)">查看</el-button>
        </template>
      </avue-crud>
      <!--<uploadDialog/>-->
      <el-dialog
        title="导入"
        v-if="uploadDialogVisible"
        :visible.sync="uploadDialogVisible"
        :before-close="handleClose"
        width="30%"
        >
<!--        <avue-form :option="uploadOption" v-model="uploadForm"  @submit="uploadSubmit"></avue-form>-->
        <el-upload
          class="upload-demo"
          ref="upload"
          action
          accept = '.pdf'
          :http-request="filesUp"
          multiple
          :on-remove="handleRemove"
          :auto-upload="true">
          <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
          <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">上传到服务器</el-button>
          <div slot="tip" class="el-upload__tip">只能上传pdf类型文件</div>
        </el-upload>
        <span slot="footer" class="dialog-footer">
  </span>
      </el-dialog>


      <el-dialog
        title="变更咨询师"
        :visible.sync="dialogVisible"
        width="30%"
      >
        <div>
          <avue-form ref="formForm" v-model="formForm" :option="formOption" @reset-change="emptytChange"
                     :upload-before="uploadBefore"
                     @submit="submit">
          </avue-form>
        </div>

      </el-dialog>
    </div>
  </div>
</template>

<script>
import tableMixins from "@/mixins/tableMixin.vue"
import uploadDialog from "@/components/uploadDialog/uploadDialog";
import axios from "axios";

export default {
  name: "resourceManagement",
  mixins: [tableMixins],
  components: {
    uploadDialog
  },
  data() {
    return {
      uploadForm: {},
      fileList:[],
      uploadOption: {
        column: [
          {
            label: '简历上传',
            prop: 'fileUrl',
            type: 'upload',
            multiple: true,
            hide: true,
            span: 24,
            propsHttp: {
              url: 'result',
              name: 'result',
            },
            tip: '支持word、pdf文件上传',
            action: '/yuanjiu/upload/uploadResumeBatch',
            // viewDisplay: false,
          },
        ]
      },
      uploadDialogVisible: false,
      options: [],
      form: {},
      data: [],
      //访问接口前缀
      url: "yuanjiu.testnew/tn-resource",
      searchForm: {
        keyWord: '',
        adminId: ""
      },
      // 表格参数
      tableOption: {
        labelWidth: 200,
        searchSpan: 6,
        border: true,
        index: false,
        indexLabel: '序号',
        indexWidth: 50,
        stripe: true,
        align: 'center',
        searchMenuSpan: 10,
        // searchLabelWidth: 100,
        calcHeight: 300,
        dialogWidth: 900,
        menuWidth: 300,
        menu: true,
        menuAlign: 'center',
        refreshBtn: false,
        columnBtn: false,
        searchShowBtn: false,
        addBtn: true,
        selection: true,
        rowKey: "id",
        viewBtn:false,
        emptyBtn: false,
        editBtn: false,
        delBtn: false,
        emptyBtnText: "重置",
        editBtnText: "修改",
        maxHeight: 645000,
        searchSize: "medium",
        column: [
          {
            prop: 'stuName',
            label: '姓名',
            rules: [{
              required: true,
              message: "请输入姓名",
              trigger: "blur"
            }],
            search: true,
            searchslot: true,
            searchLabelWidth: '80',
            overHidden: true,
          },
          {
            prop: 'adminId',
            label: '咨询师',
            hide: true,
            search: true,
            searchslot: true,
            searchLabelWidth: '80',
            editDisplay: false,
            addDisplay: false,
            overHidden: true,
          },
          {
            prop: 'priorLvl',
            label: '优先级',
            overHidden: true,
            type: "select",
            search: true,
            span: 24,
            searchLabelWidth: '80',
            props: {
              label: 'name',
              value: 'code'
            },
            dicData: [
              {
                name: '低',
                code: 1
              }, {
                name: '中',
                code: 2
              }, {
                name: '高',
                code: 3
              }],
            rules: [{
              required: true,
              message: "请选择优先级",
              trigger: "change"
            }]
          },
          {
            prop: 'productId',
            label: '培训类别',
            overHidden: true,
            type: "select",
            dicUrl: '/yuanjiu.testnew/tn-product/listTnProduct',
            dicMethod: 'post',
            props: {
              label: "productName",
              value: "productId",
              res: "result"
            },
            search: true,
            searchLabelWidth: '80',
            rules: [{
              required: true,
              message: "请选择培训类别",
              trigger: "change"
            }]
          },
          {
            prop: 'flowNum',
            label: '跟踪次数',
            addDisabled: true,
            editDisabled: true,
            overHidden: true,
          },

          {
            prop: 'area',
            label: '地域',
            overHidden: true,
          },
          // {
          //   prop: 'chanelCode',
          //   label: '渠道',
          //   search:true,
          //   searchLabelWidth:'80',
          //   type: "select",
          //   dicUrl: '/yuanjiu.testnew/yj-config/getConfigByCode?itmCode=CHANEL_CODE',
          //   props: {
          //     label: "itmName",
          //     value: "itmVal",
          //     res: "result"
          //   },
          //   rules: [{
          //     required: true,
          //     message: "请选择渠道",
          //     trigger: "change"
          //   }]
          // },
          {
            prop: 'sourceCode',
            label: '来源途径',
            type: "select",
            overHidden: true,
            dicUrl: '/yuanjiu.testnew/yj-config/getConfigByCode?itmCode=SOURCE_CODE',
            props: {
              label: "itmName",
              value: "itmVal",
              res: "result"
            },
          },
          {
            prop: 'status',
            label: '进展情况',
            type: "select",
            search: true,
            searchLabelWidth: '80',
            dicUrl: '/yuanjiu.testnew/yj-config/getConfigByCode?itmCode=PRO_STS',
            props: {
              label: "itmName",
              value: "itmVal",
              res: "result"
            }
          },
          {
            prop: 'allocTime',
            label: '分配时间',
            type: "date",
            addDisabled: true,
            editDisabled: true,
            overHidden: true,
            format: 'yyyy-MM-dd',
            valueFormat: 'yyyy-MM-dd',
          },
          {
            prop: 'flowNum',
            label: '电销',
            overHidden: true,
          },

          {
            prop: 'phoneNum',
            label: '手机号',
            overHidden: true,
            rules: [{
              required: true,
              message: "请输入手机号",
              trigger: "blur"
            }],

          },


          {
            prop: 'age',
            label: '年龄',
            overHidden: true,
            hide: true
          }, {
            prop: 'sexCd',
            label: '性别',
            hide: true,
            type: 'select',
            overHidden: true,
            props: {
              label: 'name',
              value: 'code'
            },
            dicData: [{
              name: '男',
              code: 1
            }, {
              name: '女',
              code: 2
            }],
            rules: [{
              required: true,
              message: "请选择性别",
              trigger: "change"
            }]

          }, {
            prop: 'school',
            label: '学校',
            hide: true

          }, {
            prop: 'major',
            label: '专业',
            hide: true

          },
          {
            prop: 'education',
            label: '学历', hide: true

          },
          {
            prop: 'wechat',
            label: '学员微信', hide: true

          },
          {
            prop: 'mailAddr',
            label: '学员邮箱', hide: true
          }, {
            prop: 'contactAddr',
            label: '联系地址', hide: true

          }, {
            prop: 'classId',
            label: '入学班级', hide: true,
            type: "select",
            dicMethod: 'post',
            dicUrl: '/yuanjiu.testnew/tn-class/listTnClass',
            props: {
              label: "className",
              value: "classId",
              res: "result"
            },
          },
        ]
      },
      listUrl: 'pageTnResouce',
      delUrl: "deleteList",
      delIdList: [],
      dialogVisible: false,
      formForm: {},
      formOption: {
        column: [
          {
            label: "绑定产品",
            prop: "productId",
            type: "select",
            span: 24,
            labelWidth: 100,
            dicUrl: '/yuanjiu.testnew/tn-product/listTnProduct',
            dicMethod: 'post',
            props: {
              label: 'productName',
              value: 'productId',
              res: 'result'
            },
            rules: [{
              required: true,
              message: "请选择绑定产品",
              trigger: "change"
            }],
            filterable: true
          },
          {
            label: "变更咨询师",
            prop: "adminId",
            type: "select",
            labelWidth: 100,
            span: 24,
            dicUrl: '/yuanjiu.testnew/yj-admin/getALLCounselor',
            dicMethod: 'get',
            props: {
              label: 'adminRealName',
              value: 'adminId',
              res: 'result'
            },
            rules: [{
              required: true,
              message: "请选择咨询师",
              trigger: "change"
            }],
            filterable: true
          },
          {
            label: "变更原因",
            labelWidth: 100,
            prop: "yaunying",
            type: "textarea",
            span: 24,
            rules: [{
              required: true,
              message: "请输入变更原因",
              trigger: "blur"
            }],
          }
        ]
      },
      updateUrl: "insertOrUpdate",
      newFiles:[],
      newArr:[],
      formData: new FormData()
    }
  },
  methods: {
    caozuo(){
      // console.log('caozuo',this)
    },
    handleRemove(){},
    submitUpload(){
      this.$message.warning('请稍等片刻，正在上传，请勿操作')
      this.$httpService.post("/yuanjiu/upload/uploadResumeBatch",this.formData,{headers: { files: true }}).then(res => {
        this.$message.success('上传成功')
        this.uploadDialogVisible= false
        this.formData = new FormData()
        this.getList()
      })
    },
    filesUp(content){

      this.formData.append("files",content.file,content.file.name)
    },
    handleClose(){
      // console.log('this.formData',)
      this.formData = new FormData()
      this.uploadDialogVisible  =false
    },
    uploadBefore(){},
    uploadSubmit(form,done){
      // console.log('2222',form)
      this.uploadDialogVisible  =false
      done()
      // if(form.fileUrl.length<1){
      //   this.$message.error('请上传简历')
      //   loading();
      // }
      // else {
      //   let files =[]
      //   form.fileUrl.forEach((x)=>{
      //     files.push(x.value)
      //   })
      //   this.$httpService.post("/yuanjiu/upload/uploadResumeBatch" , {
      //     files:files
      //   }).then(res => {
      //     this.$message.success('新增成功');
      //     this.getList();
      //     done();
      //     loading();
      //   }).catch(res => {
      //     loading();
      //   })
      // }
      // done()
    },
    //获取咨询师
    getZxs() {
      this.$httpService.get("/yuanjiu.testnew/yj-admin/getALLCounselor").then(res => {
        // console.log('咨询师', res)
        this.options = res
      })
    },


    /**
     * 获取列表数据
     */
    getList() {
      this.tableLoading = true;
      let query = {}
      for (let i in this.searchForm) {
        if (typeof (this.searchForm[i]) !== "undefined" && this.searchForm[i].length == 0) {
          delete this.searchForm[i]
        }
      }
      query = Object.assign({}, this.searchForm, this.page, this.search);
      query.current = this.page.currentPage
      query.size = this.page.pageSize
      if ((this.searchForm.createTime && this.searchForm.createTime.length == 2)) {
        query.startTime = this.searchForm.createTime[0]
        query.endTime = this.searchForm.createTime[1]
      }
      if ((this.searchForm.changeTime && this.searchForm.changeTime.length == 2)) {
        query.startTime = this.searchForm.changeTime[0]
        query.endTime = this.searchForm.changeTime[1]
      }
      // console.log('222222', query)
      this.$httpService.post("/" + this.url + "/" + this.listUrl, query).then(res => {
        this.tableData = res.records
        this.page.total = res.total;
        this.tableLoading = false
      })
    },


    uploadExcel() {
      this.uploadDialogVisible = true
    },
    changeConsultant(row) {
      this.stuId = row.stuId
      this.dialogVisible = true
    },


    //勾选获取当前数据ID
    selectionChange(list) {
      // console.log(list)
      this.delIdList = []
      list.forEach((x) => {
        this.delIdList.push(x.stuId)
      })
    },
    /**
     * 删除
     * @param row 行数据
     * @param index 行索引
     */
    rowDel: function (row) {
      if (this.delIdList.length == 0) {
        this.$message.error('请先选择数据');
      } else {
        this.$confirm('是否确认删除？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$httpService.post("/" + this.url + "/" + this.delUrl, this.delIdList).then(res => {
            this.$message.success('删除成功');
            this.getList();
          })
        })
      }
    },
    //抓取
    grab() {
      this.$httpService.post('/yuanjiu.testnew/tn-resource/refreshResource').then(res => {
        this.$message.success('抓取成功');
        this.getList();
      })
    },
    emptytChange() {
    },
    submit() {
      this.formForm.stuId = this.stuId
      this.$httpService.post('/yuanjiu.testnew/tn-change-record/changeCounselor', this.formForm).then(res => {
        this.$message.success('变更成功');
      })
      this.dialogVisible = false
      this.getList();
    },


  },
  mounted() {
    this.getZxs()
  }

}
</script>

<style scoped>
.inputStyle {
  width: 600px;
}

@media only screen and (max-width: 1600px) {
  .inputStyle {
    width: 530px;
  }
}

@media only screen and (max-width: 1500px) {
  .inputStyle {
    width: 430px;
  }
}

@media only screen and (max-width: 1366px) {
  .inputStyle {
    width: 330px;
  }
}

@media only screen and (max-width: 900px) {
  .inputStyle {
    width: 600px;
  }
}

</style>
